package com.zelin.gulimall.ware.service.impl;

import com.zelin.common.utils.R;
import com.zelin.gulimall.ware.entity.PurchaseDetailEntity;
import com.zelin.gulimall.ware.feign.ProductFeignService;
import com.zelin.gulimall.ware.vo.SkuHasStockVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.gulimall.ware.dao.WareSkuDao;
import com.zelin.gulimall.ware.entity.WareSkuEntity;
import com.zelin.gulimall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    private ProductFeignService productFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        /**
         * skuId:
         * wareId:
         */
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        //1.得到请求参数
        String skuId = (String) params.get("skuId");
        String wareId = (String) params.get("wareId");
        //2.得到参数是否有值判断
        if(StringUtils.isNotBlank(skuId)){
            wrapper.eq("sku_id",skuId);
        }
        if(StringUtils.isNotBlank(wareId)){
            wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 采购的商品入库
     * @param detailEntity
     */
    @Override
    public void addStock(PurchaseDetailEntity detailEntity) {
        //1.判断是否有指定记录(根据商品id及仓库id判断)
        List<WareSkuEntity> wareSkuEntities = this.baseMapper.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", detailEntity.getSkuId()).eq("ware_id", detailEntity.getWareId()));
        //2.没有记录就新建
        if(wareSkuEntities == null || wareSkuEntities.size() == 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(detailEntity.getSkuId());
            wareSkuEntity.setStock(detailEntity.getSkuNum());
            wareSkuEntity.setStockLocked(0);
            wareSkuEntity.setWareId(detailEntity.getWareId());

            //3.在过程通过skuId查询skuName
            R info = productFeignService.info(detailEntity.getSkuId());
            Map<String,Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
            if(info.getCode() == 0){    //成功访问
                String skuName = skuInfo.get("skuName") + "";   //得到商品名称
                wareSkuEntity.setSkuName(skuName);                  //设置商品够狠
            }
            this.baseMapper.insert(wareSkuEntity);
        }else{
            this.baseMapper.addStock(detailEntity);
        }

    }

    /**
     * 是否有库存
     * @param skuIds
     * @return
     */
    @Override
    public List<SkuHasStockVo> hasStock(List<Long> skuIds) {
        List<SkuHasStockVo> collect = skuIds.stream().map(skuId -> {
            Long count = this.baseMapper.selectStock(skuId);
            SkuHasStockVo stockVo = new SkuHasStockVo();
            stockVo.setSkuId(skuId);
            stockVo.setHasStock(count == null ? false : count > 0);
            return stockVo;
        }).collect(Collectors.toList());
        return collect;
    }

}